package com.test19_多线程;

/**
 * 多线程运行状态：
 * 1、新建状态
 * 2、就绪状态
 * 3、运行状态
 * 4、阻塞状态
 * 5、终止状态
 */
public class Demo06_ThreadState {

    public static void main(String[] args) throws Exception {
//        test1();
//        test2();
//        test3();
//        test4();
        test5();
    }

    /**
     * 线程休眠，Thread.sleep()
     */
    public static void test1() {
        for (int x = 0; x < 10; x++) {
            new Thread(() -> {
                for (int y = 0; y < 100; y++) {
                    try {
                        Thread.sleep(1000);     // 延迟1秒
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("【" + Thread.currentThread().getName() + "】num = " + y);
                }
            }, "Yootk线程-" + x).start();
        }
    }

    /**
     * 线程中断，thread.interrupt()
     */
    public static void test2() throws Exception {
        Thread thread = new Thread(() -> {
            try {
                System.out.println("【" + Thread.currentThread().getName() + "】准备进入到休眠状态，预计的休眠时间为20秒 ...");
                Thread.sleep(20000);    // 需要休眠20秒的时间
                System.out.println("【" + Thread.currentThread().getName() + "】休眠状态正常结束 ...");
            } catch (InterruptedException e) {
                System.out.println("【" + Thread.currentThread().getName() + "】休眠产生了异常，无法正常完成休眠处理 ...");
            }
        }, "休眠线程");
        thread.start();
        System.out.println("【中断状态】" + thread.isInterrupted());
        Thread.sleep(2000);
        thread.interrupt();     // 打断当前线程的休眠状态
        System.out.println("【中断状态】" + thread.isInterrupted());
    }

    /**
     * 线程的强制执行，thread.join()
     */
    public static void test3() throws Exception {
        Thread mainThread = Thread.currentThread();
        Thread joinThread = new Thread(() -> {
            for (int x = 0; x < 10000; x++) {
                try {
                    Thread.sleep(100);
                    if (x >= 10) {
                        mainThread.join();  // 子线程要交出全部的资源给主线程
                    }
                    System.out.println("【" + Thread.currentThread().getName() + "】子线程执行, x = " + x);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "工作线程");
        joinThread.start();
        for (int x = 0; x < 50; x++) {
            Thread.sleep(100);
            System.out.println("【" + Thread.currentThread().getName() + "】主线程执行, x = " + x);
        }
    }

    /**
     * 线程礼让，Thread.yield()
     */
    public static void test4() throws Exception {
        Thread joinThread = new Thread(() -> {
            for (int x = 0; x < 100; x++) {
                try {
                    if (x % 2 == 0) {
                        Thread.yield();     // 礼让一次
                        System.out.println("【YIELD】线程礼让执行。");
                    }
                    Thread.sleep(100);
                    System.out.println("【" + Thread.currentThread().getName() + "】子线程执行, x = " + x);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "工作线程");
        joinThread.start();
        for (int x = 0; x < 50; x++) {
            Thread.sleep(100);
            System.out.println("【" + Thread.currentThread().getName() + "】主线程执行, x = " + x);
        }
    }

    /**
     * 线程优先级
     * 线程优先级越高，越有可能先执行
     * 默认优先级：中等优先级(5)
     */
    public static void test5() {
        System.out.println("【主线程的优先级】" + Thread.currentThread().getPriority());
        Thread[] threads = new Thread[3];   // 创建线程数组
        for (int x = 0; x < threads.length; x++) {
            threads[x] = new Thread(() -> {
                while (true) {
                    try {
                        Thread.sleep(200);
                        System.out.println("【" + Thread.currentThread().getName() + "】线程执行，线程优先级："+ Thread.currentThread().getPriority());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        threads[0].setPriority(Thread.MIN_PRIORITY);    // 最低优先级
        threads[1].setPriority(Thread.MAX_PRIORITY);    // 最高优先级
        threads[2].setPriority(Thread.MIN_PRIORITY);
        for (int x = 0; x < threads.length; x++) {
            threads[x].start();
        }
    }
}
